home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / SHELL_SO / SHELL.C < prev    next >
Text File  |  1992-12-02  |  10KB  |  411 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45. *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 030690    :    cleaned up add shell string
  54.  *    SPK 030590    :    Added command history
  55.  *    SPK 012290    :    Initial
  56.  */
  57.  
  58. #include    <WindowMgr.h>
  59. #include    "System.h"
  60. #include    "Global.h"
  61. #include    "Struct.h"
  62. #include    "Shell.h"
  63. #include    "Path.h"
  64. #include    "Mac.h"
  65.  
  66. #include    "Prefs.h"
  67.  
  68. /*******************************************************************
  69.  *    Shell command history FUNCTIONS    
  70.  *******************************************************************/
  71.  
  72. void            RecordCmd( char *string, WHandle ShellWh )
  73. {
  74. char            str[ 32 ], *cp, save;
  75. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  76.  
  77.     ShellSetVar( ShellWh, "COMMAND", string );
  78.     
  79. /* save new command */
  80.     (**MyShell).lastCmdNum++;
  81.     sprintf( str, "%d_CMD", (**MyShell).lastCmdNum );
  82.  
  83. /* remove and crs */
  84.     save = '\0';
  85.     cp = string;
  86.     while( *cp )
  87.         {
  88.         if( *cp == '\r'|| *cp == '\n' )
  89.             {
  90.             save = *cp;
  91.             *cp = '\0';
  92.             break;
  93.             }
  94.         cp++;
  95.         }
  96.  
  97.     ShellSetVar( ShellWh, str, string );
  98.  
  99.     if( save == '\r'|| save == '\n' )
  100.         *cp = save;
  101.  
  102. /* reset current command */
  103.     (**MyShell).currCmdNum = (**MyShell).lastCmdNum;
  104. /* first ever ? */
  105.     if( (**MyShell).firstCmdNum == 0 )
  106.         (**MyShell).firstCmdNum = (**MyShell).currCmdNum;
  107.         
  108. /* To many commands saved in history ? */
  109.     if( ((**MyShell).lastCmdNum - (**MyShell).firstCmdNum) > MAXCMDHISTORY )
  110.         {
  111.         sprintf( str, "%d_CMD", (**MyShell).firstCmdNum );
  112.         ShellDelVar( ShellWh, str );
  113.         (**MyShell).firstCmdNum++;
  114.         }
  115. }
  116.  
  117. void    ResetCmdInput( char *string, WHandle ShellWh )
  118. {
  119. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  120. PEHandle        hPE;
  121. int32            len;
  122.  
  123.     hPE = (**MyShell).ihPE;
  124.     len = (int32) strlen( string );
  125.  
  126.     PESetSelect( (**MyShell).promptBase, (**hPE).peLength, hPE );
  127.     PEDelete( hPE );
  128.     PEInsert( string, len, hPE );    
  129.     PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
  130. }
  131.  
  132. void    LastCmd( WHandle ShellWh )
  133. {
  134. char            str[ 32 ];
  135. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  136.  
  137. /* is their a command available */
  138.     if( (**MyShell).lastCmdNum > 0 )
  139.         {
  140.         sprintf( str, "%d_CMD", (**MyShell).currCmdNum );
  141.         ResetCmdInput( ShellGetVar( ShellWh, str ), ShellWh );
  142.  
  143.         (**MyShell).currCmdNum--;
  144.  
  145.         if( (**MyShell).currCmdNum < (**MyShell).firstCmdNum )
  146.             (**MyShell).currCmdNum = (**MyShell).lastCmdNum;
  147.         }
  148. }
  149.  
  150. void    NextCmd( WHandle ShellWh )
  151. {
  152. char            str[ 32 ];
  153. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  154.  
  155. /* is their a command available */
  156.     if( (**MyShell).lastCmdNum > 0 )
  157.         {
  158.         sprintf( str, "%d_CMD", (**MyShell).currCmdNum );
  159.         ResetCmdInput( ShellGetVar( ShellWh, str ), ShellWh );
  160.  
  161.         (**MyShell).currCmdNum++;
  162.  
  163.         if( (**MyShell).currCmdNum > (**MyShell).lastCmdNum )
  164.             (**MyShell).currCmdNum = (**MyShell).firstCmdNum;
  165.         }
  166. }
  167.  
  168. /*******************************************************************/
  169.  
  170. void    ClearShellOutput( WHandle ShellWh )
  171. {
  172. PEHandle        hPE;
  173. ShellWindRec    **MyShell;    
  174.     
  175.     MyShell = (ShellWindRec **) ((**ShellWh).thing);
  176.     
  177.     hPE = (**MyShell).hPE;
  178.     
  179.     PESetSelect( 0, 200000L, hPE );
  180.     PEDelete( hPE );
  181.     SetPeditEditStatus( hPE, ShellWh );
  182.     SetUndo( hPE );
  183. }
  184.  
  185. /*******************************************************************/
  186.  
  187. char    promptStr[ 64 ];
  188.  
  189. char    *GetPromptStr( WHandle ShellWh )
  190. {
  191. char            *cp;
  192. ShellWindRec    **MyPedit;    
  193.  
  194.     *promptStr = '\0';
  195.     
  196.     if( (**ShellWh).WindAppType == ShellWindowID )
  197.         {
  198.         MyPedit = (ShellWindRec **) ((**ShellWh).thing);
  199.     
  200.         if( (**MyPedit).prompt )
  201.             cp = ShellGetVar( ShellWh, "PS2" );
  202.         else
  203.             cp = ShellGetVar( ShellWh, "PS1" );
  204.     
  205.         strcpy( promptStr, cp );
  206.         ExpandShellStr( ShellWh, promptStr );
  207.         }
  208.         
  209.     return( promptStr );
  210. }
  211.  
  212. void            DoShellPrompt( WHandle ShellWh )
  213. {
  214. ShellWindRec    **MyPedit;    
  215. char            *cp;
  216. int32            ss, se, pb, base = 1;
  217.  
  218.     if( (**ShellWh).WindAppType == ShellWindowID )
  219.         {
  220.         MyPedit = (ShellWindRec **) ((**ShellWh).thing);
  221.  
  222.         cp = GetPromptStr( ShellWh );
  223.         (**MyPedit).promptBase = strlen( cp );
  224.             
  225.         ss = (**(**MyPedit).ihPE).selStart;
  226.         se = (**(**MyPedit).ihPE).selEnd;
  227.         pb = (**MyPedit).promptBase;
  228.         
  229.         PESetSelect( 0L, 0L, (**MyPedit).ihPE );
  230.         PEInsert( cp, pb, (**MyPedit).ihPE ); 
  231.         PESetSelect( ss+pb, se+pb, (**MyPedit).ihPE );
  232.         }
  233. }
  234.  
  235. /*******************************************************************/
  236.  
  237. void            CleanShellInput( WHandle ShellWh )
  238. {
  239. ShellWindRec    **MyPedit;    
  240. char            *cp;
  241. int32            ss, se, pb, base = 1;
  242.  
  243.     if( (**ShellWh).WindAppType == ShellWindowID )
  244.         {
  245.         MyPedit = (ShellWindRec **) ((**ShellWh).thing);
  246.             
  247.         PESetSelect( 0L, (**(**MyPedit).ihPE).selEnd, (**MyPedit).ihPE );
  248.         PEDelete( (**MyPedit).ihPE );
  249.         }
  250. }
  251.  
  252. /*******************************************************************/
  253.  
  254. WHandle        currShell = NULL;        /* Window Handler Handle */
  255.  
  256. void    SetShellName( WHandle ShellWh, char *name )
  257. {
  258. char    buf[ 256 ];
  259.  
  260.     if( name )
  261.         strcpy( buf, name );
  262.     else
  263.         GetCurrPath( buf );
  264.         
  265.     CtoPstr( buf );
  266.     SetWTitle( (**ShellWh).whWind, buf );
  267. }
  268.  
  269. /*******************************************************************/
  270.  
  271. static    int        numShells = 1;
  272.  
  273. void    ShellWindInit( char *windowName )
  274. {
  275. WHandle            ShellWh = NULL;        /* Window Handler Handle */
  276. ShellWindRec    **MyShell;
  277. int16             i;
  278. int32             len = sizeof( ShellWindRec );
  279. int32            free;
  280.  
  281.     free = FreeMem();
  282. /*
  283.  *    Check for sufficient memeory
  284.  */
  285.     if( free < (sizeof( ShellWindRec ) + 8192L ) )
  286.         {
  287.         printf( "Not enough memory\n" );
  288.         SysBeep( 1 );
  289.         return;
  290.         }
  291.  
  292.     ShellWh = TextEditInit( ShellWindowID, windowName ); /* true, it is the Shell Window */
  293.     SetShellName( ShellWh, NULL );
  294.     MyShell = (ShellWindRec **) (**(ShellWh)).thing;
  295.  
  296.      (**ShellWh).MenuState.Frevert    =    0; /* no revert for the Shell window */
  297.      (**ShellWh).MenuState.Fsave        =    0; /* no save for the Shell window - use save as... */
  298.  
  299.     if( GetWHandler( FrontWindow() ) != ShellWh )
  300.         TeWActivate( FALSE, ShellWh );     /* de-highlight scrollbars... */
  301.  
  302.     (**ShellWh).whFrontOnly = true;    /* true if idle only when in front */
  303.     ( **MyShell ).shellID         = numShells;
  304.     
  305.     ( **MyShell ).firstCmdNum     = 0;
  306.     ( **MyShell ).lastCmdNum     = 0;
  307.     ( **MyShell ).currCmdNum     = 0;
  308.     
  309.     GetPWDInfo( &((**MyShell).pwdVRefNum), &((**MyShell).pwdDirID),
  310.         &((**MyShell).parDirID) );
  311.     
  312.     InitShellVars( ShellWh );
  313.     InitProcMgr( ShellWh );
  314.     DoShellPrompt( ShellWh );
  315.     
  316.     printDemoMess();
  317. /*
  318.  *    Execute the '.profile' file
  319.  */
  320.     if( (numShells == 1 && ShellPrefs.useProfileStartup) 
  321.         || (numShells > 1 && ShellPrefs.useProfileNewShell) )
  322.         {
  323.         i = ScanDir( ".profile", &((**MyShell).pwdVRefNum),
  324.             &((**MyShell).pwdDirID), &((**MyShell).parDirID) );
  325.             
  326.         if( i == pathIsFile )
  327.             {
  328.             CommandString( ".profile\n", ShellWh );
  329.             }
  330.         }
  331.         
  332.     numShells++;
  333.     SetWD( (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  334.  
  335. }
  336.  
  337. /*******************************************************************/
  338.  
  339. void    ResetShellPWD( WHandle ShellWh )
  340. {
  341. ShellWindRec    **MyShell;
  342.  
  343.     if( ShellWh && ((**ShellWh).WindAppType == ShellWindowID ))
  344.         {
  345.         MyShell = (ShellWindRec **) (**(ShellWh)).thing;
  346.  
  347.         SetWD( (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  348.         SetShellName( ShellWh, NULL );
  349.         }
  350. }
  351.  
  352. /*******************************************************************/
  353.  
  354. void    ShellAddStr( s, ShellWh )
  355. char        *s;
  356. WHandle        ShellWh;        /* Window Handler Handle */
  357. {
  358. int32            len;
  359. PeditWindRec    **MyPedit;    
  360. GrafPtr            port;
  361. int32            free;
  362. PEHandle        hPE;
  363.  
  364.     free = FreeMem();
  365.     GetPort( &port);
  366.     
  367.     if( ShellWh == NULL )
  368.         ShellWh = currShell;
  369.  
  370.     if( ShellWh == NULL )
  371.         return;
  372.  
  373.     MyPedit = (PeditWindRec **) ((**ShellWh).thing);
  374.     SetPort( (**ShellWh).whWind);
  375.     hPE = (**MyPedit).hPE;
  376.     
  377.     /* will this insert overflow memory ? */
  378.     len = (int32) strlen( s );
  379.     if( free < len )
  380.         {
  381.         PESetSelect( 0L, len, hPE );
  382.         PEDelete( hPE );
  383.         }
  384.  
  385.     PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
  386.     PEInsert( s, len, hPE ); 
  387.  
  388.     if( (**MyPedit).inInput )
  389.         {
  390.         (**MyPedit).inInput = FALSE;
  391.         AdjustVScroll( ShellWh, true ); 
  392.         (**MyPedit).inInput = TRUE;
  393.         }
  394.     else
  395.         AdjustVScroll( ShellWh, true ); 
  396.  
  397.     /* have we grown past the maximum character specification ? */
  398.     if( (**hPE).peLength > ShellPrefs.ShellCharLimit )
  399.         {
  400.         int32    cutPoint;
  401.         
  402.         cutPoint = (**hPE).peLength - (ShellPrefs.ShellCharLimit - 2048);
  403.         cutPoint = PEBol( cutPoint, hPE );
  404.         PESetSelect( 0L, cutPoint, hPE );
  405.         PEDelete( hPE );
  406.         PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
  407.         }
  408.  
  409.  
  410.     SetPort( port );
  411. }